home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / campbell / gutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  4.9 KB  |  232 lines

  1. /* GNUPLOT - util.c */
  2. /*
  3.  * Copyright (C) 1986, 1987, 1990, 1991   Thomas Williams, Colin Kelley
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software is provided "as is" without express or implied warranty.
  16.  * 
  17.  *
  18.  * AUTHORS
  19.  * 
  20.  *   Original Software:
  21.  *     Thomas Williams,  Colin Kelley.
  22.  * 
  23.  *   Gnuplot 2.0 additions:
  24.  *       Russell Lang, Dave Kotz, John Campbell.
  25.  *
  26.  *   Gnuplot 3.0 additions:
  27.  *       Gershon Elber and many others.
  28.  * 
  29.  * Send your comments or suggestions to 
  30.  *  pixar!info-gnuplot@sun.com.
  31.  * This is a mailing list; to join it send a note to 
  32.  *  pixar!info-gnuplot-request@sun.com.  
  33.  * Send bug reports to
  34.  *  pixar!bug-gnuplot@sun.com.
  35.  */
  36.  
  37. #include <ctype.h>
  38. #include <setjmp.h>
  39. #include <stdio.h>
  40. #include <errno.h>
  41. #include "plot.h"
  42.  
  43. BOOLEAN screen_ok;
  44.     /* TRUE if command just typed; becomes FALSE whenever we
  45.         send some other output to screen.  If FALSE, the command line
  46.         will be echoed to the screen before the ^ error message. */
  47.  
  48. #ifndef vms
  49. #ifndef __ZTC__
  50. extern int errno;
  51. extern int sys_nerr;
  52. extern char *sys_errlist[];
  53. #endif
  54. #endif /* vms */
  55.  
  56. extern char input_line[];
  57. extern struct lexical_unit token[];
  58. extern jmp_buf env;    /* from plot.c */
  59. extern int inline_num;        /* from command.c */
  60. extern BOOLEAN interactive;    /* from plot.c */
  61. extern char *infile_name;    /* from plot.c */
  62. extern int c_token, num_tokens;
  63.  
  64. extern char *strchr();
  65.  
  66. #ifndef AMIGA_AC_5
  67. extern double sqrt(), atan2();
  68. #endif
  69.  
  70.  
  71. /*
  72.  * equals() compares string value of token number t_num with str[], and
  73.  *   returns TRUE if they are identical.
  74.  */
  75. equals(t_num, str)
  76. int t_num;
  77. char *str;
  78. {
  79. register int i;
  80.  
  81.     if (!token[t_num].is_token)
  82.         return(FALSE);                /* must be a value--can't be equal */
  83.     for (i = 0; i < token[t_num].length; i++) {
  84.         if (input_line[token[t_num].start_index+i] != str[i])
  85.             return(FALSE);
  86.         }
  87.     /* now return TRUE if at end of str[], FALSE if not */
  88.     return(str[i] == '\0');
  89. }
  90.  
  91.  
  92.  
  93. /*
  94.  * almost_equals() compares string value of token number t_num with str[], and
  95.  *   returns TRUE if they are identical up to the first $ in str[].
  96.  */
  97. almost_equals(t_num, str)
  98. int t_num;
  99. char *str;
  100. {
  101. register int i;
  102. register int after = 0;
  103. register start = token[t_num].start_index;
  104. register length = token[t_num].length;
  105.  
  106.     if (!token[t_num].is_token)
  107.         return(FALSE);                /* must be a value--can't be equal */
  108.     for (i = 0; i < length + after; i++) {
  109.         if (str[i] != input_line[start + i]) {
  110.             if (str[i] != '$')
  111.                 return(FALSE);
  112.             else {
  113.                 after = 1;
  114.                 start--;    /* back up token ptr */
  115.                 }
  116.             }
  117.         }
  118.  
  119.     /* i now beyond end of token string */
  120.  
  121.     return(after || str[i] == '$' || str[i] == '\0');
  122. }
  123.  
  124.  
  125.  
  126. isstring(t_num)
  127. int t_num;
  128. {
  129.     
  130.     return(token[t_num].is_token &&
  131.            (input_line[token[t_num].start_index] == '\'' ||
  132.            input_line[token[t_num].start_index] == '\"'));
  133. }
  134.  
  135.  
  136. /*
  137.  * quote_str() does the same thing as copy_str, except it ignores the
  138.  *   quotes at both ends.  This seems redundant, but is done for
  139.  *   efficency.
  140.  */
  141. quote_str(str, t_num)
  142. char str[];
  143. int t_num;
  144. {
  145. register int i = 0;
  146. register int start = token[t_num].start_index + 1;
  147. register int count;
  148.  
  149.     if ((count = token[t_num].length - 2) > MAX_ID_LEN)
  150.         count = MAX_ID_LEN;
  151.     if (count>0) {
  152.         do {
  153.             str[i++] = input_line[start++];
  154.             } while (i != count);
  155.     }
  156.     str[i] = '\0';
  157. }
  158.  
  159.  
  160. /*
  161.  *    capture() copies into str[] the part of input_line[] which lies between
  162.  *    the begining of token[start] and end of token[end].
  163.  */
  164. capture(str,start,end)
  165. char str[];
  166. int start,end;
  167. {
  168. register int i,e;
  169.  
  170.     e = token[end].start_index + token[end].length;
  171.     for (i = token[start].start_index; i < e && input_line[i] != '\0'; i++)
  172.         *str++ = input_line[i];
  173.     *str = '\0';
  174. }
  175.  
  176.  
  177. convert(val_ptr, t_num)
  178. struct value *val_ptr;
  179. int t_num;
  180. {
  181.     *val_ptr = token[t_num].l_val;
  182. }
  183.  
  184.  
  185. static char *num_to_str(r)
  186. double r;
  187. {
  188.     static i = 0;
  189.     static char s[4][20];
  190.     int j = i++;
  191.  
  192.     if ( i > 3 ) i = 0;
  193.  
  194.     sprintf( s[j], "%g", r );
  195.     if ( strchr( s[j], '.' ) == NULL &&
  196.          strchr( s[j], 'e' ) == NULL &&
  197.          strchr( s[j], 'E' ) == NULL )
  198.         strcat( s[j], ".0" );
  199.  
  200.     return s[j];
  201.  
  202.  
  203.  
  204. double
  205. real(val)        /* returns the real part of val */
  206. struct value *val;
  207. {
  208.     switch(val->type) {
  209.         case INT:
  210.             return((double) val->v.int_val);
  211.         case CMPLX:
  212.             return(val->v.cmplx_val.real);
  213.     }
  214.     int_error("unknown type in real()",NO_CARET);
  215.     /* NOTREACHED */
  216.     return((double)0.0);
  217. }
  218.  
  219. struct value *
  220. const_express(valptr)
  221. struct value *valptr;
  222. {
  223.     if (END_OF_COMMAND)
  224.         int_error("constant expression required",c_token);
  225.  
  226. /* Cheap imitation, assume there is a well-formed value to avoid evaluate_at */
  227.     convert(valptr, c_token);
  228.  
  229.     return(valptr);
  230. }
  231.